/**
 *    Copyright 2009-2015 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.apache.ibatis.executor;

import java.sql.SQLException;
import java.util.List;

import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.transaction.Transaction;

/**
 * @author Clinton Begin
 */
/**
 * 执行器
 * 
 */
public interface Executor {

	/**
	 * 不需要ResultHandler
	 */
	ResultHandler NO_RESULT_HANDLER = null;

	/**
	 * 更新操作
	 * 
	 * @param ms
	 * @param parameter
	 * @return
	 * @throws SQLException
	 */
	int update(MappedStatement ms, Object parameter) throws SQLException;

	/**
	 * 查询操作，带分页，带缓存，BoundSql
	 * 
	 * @param ms
	 * @param parameter
	 * @param rowBounds
	 * @param resultHandler
	 * @param cacheKey
	 * @param boundSql
	 * @return
	 * @throws SQLException
	 */
	<E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler,
			CacheKey cacheKey, BoundSql boundSql) throws SQLException;

	/**
	 * 查询，带分页
	 * 
	 * @param ms
	 * @param parameter
	 * @param rowBounds
	 * @param resultHandler
	 * @return
	 * @throws SQLException
	 */
	<E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler)
			throws SQLException;

	/**
	 * 更新statement语句
	 * 
	 * @return
	 * @throws SQLException
	 */
	List<BatchResult> flushStatements() throws SQLException;

	/**
	 * 提交操作，参数是是否要强制
	 * 
	 * @param required
	 * @throws SQLException
	 */
	void commit(boolean required) throws SQLException;

	/**
	 * 事务回滚操作 ,参数是是否要强制
	 * 
	 * @param required
	 * @throws SQLException
	 */
	void rollback(boolean required) throws SQLException;

	/**
	 * 创建CacheKey---缓存key操作
	 * 
	 * @param ms
	 * @param parameterObject
	 * @param rowBounds
	 * @param boundSql
	 * @return
	 */
	CacheKey createCacheKey(MappedStatement ms, Object parameterObject, RowBounds rowBounds, BoundSql boundSql);

	/**
	 * 是否开启缓存
	 * 
	 * @param ms
	 * @param key
	 * @return
	 */
	boolean isCached(MappedStatement ms, CacheKey key);

	/**
	 * 清除本地缓存操作
	 */
	void clearLocalCache();

	/**
	 * 推迟加载操作
	 * 
	 * @param ms
	 * @param resultObject
	 * @param property
	 * @param key
	 * @param targetType
	 */
	void deferLoad(MappedStatement ms, MetaObject resultObject, String property, CacheKey key, Class<?> targetType);

	/**
	 * 获取事务
	 * 
	 * @return
	 */
	Transaction getTransaction();

	/**
	 * 关闭连接
	 * 
	 * @param forceRollback
	 */
	void close(boolean forceRollback);
    /**
     * 判断是否关闭
     * @return
     */
	boolean isClosed();
	/**
	 * 设置执行者包装
	 * @param executor
	 */
	void setExecutorWrapper(Executor executor);

}
